// 全局路由守卫
import { permission } from '@/permission/permission';
import { config } from '@/config/config';
import router from './index';
export default function RouterGuard() {
	// 监听浏览器输入框直接输入路径
	window.addEventListener('popstate', event => {
		let url = location.hash.replace('#', '');
		let newUrl = url.split('?');
		let role = uni.getStorageSync('userInfo').role; // 后端返回的角色
		if (!uni.getStorageSync('token')) {
			console.log('未登录');
			// 未登录
			uni.redirectTo({
				url: config.loginPage
			});
		} else {
			if (newUrl[0] === config.loginPage) {
				//登录页不做权限校验
			} else if (newUrl[0] === '/pages/index/index') {
				//首页不做权限校验
				uni.$emit('hashchange', newUrl[0]);
			} else {
				// 过滤权限
				uni.getStorageSync('permit') ? (permission.defaultRule = uni.getStorageSync('permit')) : '';
				let routers = permission.defaultRule.filter(item => {
					return item.name === role;
				});
				// console.log('routers',routers);
				// 解决一级菜单跳转空白页的问题
				let redirectUrl = router.defaultRouter.filter(item => {
					return newUrl[0] == item.path;
				});
				// console.log('redirectUrl',redirectUrl);
				if (routers.length !== 0 && routers[0].page.includes(newUrl[0])) {
					if (router.defaultRouter.length) {
						uni.redirectTo({
							url: redirectUrl[0].redirectTo
						});
						uni.$emit('hashchange', redirectUrl[0].redirectTo);
					}else{
						uni.$emit('hashchange', newUrl[0]);
					}
					
				}
				else {
					//无权限 跳转error
					uni.redirectTo({
						url: '/pages/error/error'
					});
					uni.$emit('hashchange', 'error');
				}
			}
		}
	});
}
